<?

namespace Gazelle\Torrent;


use Lang;

class Region {
    private const mapping = [
        'UK' => 'United Kingdom',
        'USA' => 'United States',
        'Russia' => 'Russian Federation',
    ];

    private const keyMapping = [
        'hong kong' => 'Hong Kong,China',
        'taiwan' => 'Taiwan,China',
        'macao' => 'Macao',
    ];

    private const postSphinxKey = [
        'Hong Kong,China' => 'hong kong',
        'Taiwan,China' => 'taiwan',
        'Macao' => 'macao',

    ];
    private const Item = [
        'afghanistan',
        'aland_islands',
        'albania',
        'algeria',
        'american_samoa',
        'andorra',
        'angola',
        'anguilla',
        'antigua_and_barbuda',
        'argentina',
        'armenia',
        'aruba',
        'australia',
        'austria',
        'azerbaijan',
        'bangladesh',
        'bahrain',
        'bahamas',
        'barbados',
        'belarus',
        'belgium',
        'belize',
        'benin',
        'bermuda',
        'bhutan',
        'bolivia',
        'bosnia_and_herzegovina',
        'botswana',
        'bouvet_island',
        'brazil',
        'brunei',
        'bulgaria',
        'burkina_faso',
        'burundi',
        'cambodia',
        'cameroon',
        'canada',
        'cape_verde',
        'central_african_republic',
        'chad',
        'chile',
        'christmas_islands',
        'cocos_(keeling)_islands',
        'colombia',
        'comoros',
        'congo_(congo-kinshasa)',
        'congo',
        'cook_islands',
        'costa_rica',
        'cote_d\'ivoire',
        'china',
        'croatia',
        'cuba',
        'czech',
        'cyprus',
        'denmark',
        'djibouti',
        'dominica',
        'ecuador',
        'egypt',
        'equatorial_guinea',
        'eritrea',
        'estonia',
        'ethiopia',
        'faroe_islands',
        'fiji',
        'finland',
        'france',
        'metropolitanfrance',
        'french_guiana',
        'french_polynesia',
        'gabon',
        'gambia',
        'georgia',
        'germany',
        'ghana',
        'gibraltar',
        'greece',
        'grenada',
        'guadeloupe',
        'guam',
        'guatemala',
        'guernsey',
        'guinea-bissau',
        'guinea',
        'guyana',
        'haiti',
        'honduras',
        'hungary',
        'iceland',
        'india',
        'indonesia',
        'iran',
        'iraq',
        'ireland',
        'isle_of_man',
        'israel',
        'italy',
        'jamaica',
        'japan',
        'jersey',
        'jordan',
        'kazakhstan',
        'kenya',
        'kiribati',
        'south_korea',
        'north_korea',
        'kuwait',
        'kyrgyzstan',
        'laos',
        'latvia',
        'lebanon',
        'lesotho',
        'liberia',
        'libya',
        'liechtenstein',
        'lithuania',
        'luxembourg',
        'macedonia',
        'malawi',
        'malaysia',
        'madagascar',
        'maldives',
        'mali',
        'malta',
        'marshall_islands',
        'martinique',
        'mauritania',
        'mauritius',
        'mayotte',
        'mexico',
        'micronesia',
        'moldova',
        'monaco',
        'mongolia',
        'montenegro',
        'montserrat',
        'morocco',
        'mozambique',
        'myanmar',
        'namibia',
        'nauru',
        'nepal',
        'netherlands',
        'new_caledonia',
        'new_zealand',
        'nicaragua',
        'niger',
        'nigeria',
        'niue',
        'norfolk_island',
        'norway',
        'oman',
        'pakistan',
        'palau',
        'palestine',
        'panama',
        'papua_new_guinea',
        'peru',
        'philippines',
        'pitcairn_islands',
        'poland',
        'portugal',
        'puerto_rico',
        'qatar',
        'reunion',
        'romania',
        'rwanda',
        'russia',
        'saint_helena',
        'saint_kitts-nevis',
        'saint_lucia',
        'saint_vincent_and_the_grenadines',
        'el_salvador',
        'samoa',
        'san_marino',
        'sao_tome_and_principe',
        'saudi_arabia',
        'senegal',
        'seychelles',
        'sierra_leone',
        'singapore',
        'serbia',
        'slovakia',
        'slovenia',
        'solomon_islands',
        'somalia',
        'south_africa',
        'spain',
        'sri_lanka',
        'sudan',
        'suriname',
        'swaziland',
        'sweden',
        'switzerland',
        'syria',
        'tajikistan',
        'tanzania',
        'thailand',
        'trinidad_and_tobago',
        'timor-leste',
        'togo',
        'tokelau',
        'tonga',
        'tunisia',
        'turkey',
        'turkmenistan',
        'tuvalu',
        'uganda',
        'ukraine',
        'united_arab_emirates',
        'united_kingdom',
        'united_states',
        'uruguay',
        'uzbekistan',
        'vanuatu',
        'vatican_city',
        'venezuela',
        'vietnam',
        'wallis_and_futuna',
        'western_sahara',
        'yemen',
        'yugoslavia',
        'zambia',
        'zimbabwe',
        'taiwan',
        'hong_kong',
        'macao',
        'soviet_union',
    ];

    public static function allItem($Type = null) {
        return self::Item;
    }

    private static function map($Region) {
        if (array_key_exists($Region, self::mapping)) {
            return self::mapping[$Region];
        }
        return $Region;
    }

    private static function keyMap($Key) {
        if (array_key_exists($Key, self::keyMapping)) {
            return self::keyMapping[$Key];
        }
        return $Key;
    }

    public static function text($Item) {
        $value = Region::map($Item);
        return t('server.country.' . str_replace(' ', '_', trim(strtolower($value))), ['DefaultValue' => $Item]);
    }
    public static function sphinx_key($Text) {
        $Text = Region::keyMap($Text);
        $key = Lang::get_key('server.country', $Text);
        if (!empty($key)) {
            $Ret = Lang::getWithLang($key, Lang::EN);
            if (array_key_exists($Ret, self::postSphinxKey)) {
                return self::postSphinxKey[$Ret];
            }
            return $Ret;
        }
        return 'invalid';
    }
}
